<template>
  <el-dialog v-model="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false" :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="rules" ref="dataFormRef" @keyup.enter="dataFormSubmitHandle()" label-width="120px">
      <el-form-item label="题目" prop="title">
        <el-input v-model="dataForm.title" placeholder="题目"></el-input>
      </el-form-item>
      <el-form-item label="摘要" prop="abstracts" style="height: 420px">
        <WangEditor v-model="dataForm.content"></WangEditor>
      </el-form-item>
      <el-form-item label="关键词" prop="keyword">
        <el-input v-model="dataForm.keyword" placeholder="关键词"></el-input>
      </el-form-item>
      <el-form-item label="论文作者" prop="author">
        <el-input v-model="dataForm.author" placeholder="论文作者"></el-input>
      </el-form-item>
      <el-form-item label="论文正文" prop="url">
<!--        <el-input v-model="dataForm.url" placeholder="论文正文地址"></el-input>-->
        <el-button type="primary" @click="addFile(dataForm.id)">点击上传论文文件</el-button>
      </el-form-item>
      <el-form-item label="是否出版" prop="publishStatus">
<!--        <el-input v-model="dataForm.publishStatus" placeholder="是否出版(0：已出版1：未出版)"></el-input>-->
        <el-select v-model="dataForm.publishStatus" placeholder="是否出版">
          <el-option v-for="item in state.publishText" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="论文出版时间" prop="publicationDate" v-show="dataForm.publishStatus==='0'">
        <el-date-picker type="datetime" placeholder="论文出版时间" v-model="dataForm.publicationDate" format="YYYY-MM-DD hh:mm:ss" value-format="YYYY-MM-DD hh:mm:ss"></el-date-picker>
      </el-form-item>
      <el-form-item label="DOI" prop="doi" v-show="dataForm.publishStatus==='0'">
        <el-input v-model="dataForm.doi" placeholder="DOI"></el-input>
      </el-form-item>
      <el-form-item label="期刊" prop="journal" v-show="dataForm.publishStatus==='0'">
        <el-input v-model="dataForm.journal" placeholder="期刊"></el-input>
      </el-form-item>
      <el-form-item label="页数" prop="pageNumber" v-show="dataForm.publishStatus==='0'">
<!--        <el-input v-model="dataForm.pageNumber" placeholder="页数"></el-input>-->
        <el-input-number v-model="dataForm.pageNumber" :min="1" />
      </el-form-item>
      <el-form-item label="期" prop="period" v-show="dataForm.publishStatus==='0'">
<!--        <el-input v-model="dataForm.period" placeholder="期"></el-input>-->
        <el-input-number v-model="dataForm.period" :min="1" />
      </el-form-item>
      <el-form-item label="卷" prop="volume" v-show="dataForm.publishStatus==='0'">
<!--        <el-input v-model="dataForm.volume" placeholder="卷"></el-input>-->
        <el-input-number v-model="dataForm.volume" :min="1" />
      </el-form-item>
      <el-form-item label="通讯地址" prop="address">
        <el-input v-model="dataForm.address" placeholder="通讯地址"></el-input>
      </el-form-item>
    </el-form>
    <template v-slot:footer>
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t("confirm") }}</el-button>
    </template>
  </el-dialog>
  <upload v-if="uploadVisible" ref="upload"  @fileList="getUrl"></upload>
</template>

<script lang="ts" setup>
import {nextTick, reactive, ref} from "vue";
import baseService from "@/service/baseService";
import WangEditor from "@/components/wang-editor/index.vue";
import { useI18n } from "vue-i18n";
import { ElMessage } from "element-plus";
import Upload from "@/views/poap/tfile-upload.vue";

const { t } = useI18n();
const emit = defineEmits(["refreshDataList"]);

const visible = ref(false);
const uploadVisible = ref(false)
const dataFormRef = ref();

const dataForm = reactive({
  id: "",
  title: "",
  abstracts: "",
  content:"",
  keyword: "",
  author: "",
  url: "",
  publishStatus: "",
  publicationDate: "",
  doi: "",
  journal: "",
  pageNumber: "",
  period: "",
  volume: "",
  delFlag: "",
  createDate: "",
  creator: "",
  updateDate: "",
  updater: "",
  address: "",
});

const state = reactive({
  publishText:[
    {value:0,label:"已出版"},
    {value:1,label:"未出版"},
  ]
})
const rules = ref({
});

const init = (id?: number) => {
  visible.value = true;
  dataForm.id = "";

  // 重置表单数据
  if (dataFormRef.value) {
    dataFormRef.value.resetFields();
  }

  if (id) {
    getInfo(id);
  }
};

// 获取信息
const getInfo = (id: number) => {
  baseService.get("/poap/poappage/" + id).then((res) => {
    Object.assign(dataForm, res.data);
  });
};

const upload = ref()
const addFile = (id:string)=>{
  uploadVisible.value = true;
  nextTick(()=>{
    upload.value.init()
  })
}
/**
 *
 获取文件地址
 * @param e
 */
const getUrl = (e:any)=> {
  console.log(e);
  dataForm.url=e[0].response.data.url
  // dataForm.fileName = dataForm.fileName + " | " + dataForm.url[0].name;
}

// 表单提交
const dataFormSubmitHandle = () => {
  dataFormRef.value.validate((valid: boolean) => {
    if (!valid) {
      return false;
    }
    (!dataForm.id ? baseService.post : baseService.put)("/poap/poappage", dataForm).then((res) => {
      ElMessage.success({
        message: t("prompt.success"),
        duration: 500,
        onClose: () => {
          visible.value = false;
          emit("refreshDataList");
        }
      });
    });
  });
};

defineExpose({
  init
});
</script>
